[contents] [next] [bottom] (1 out of 4)

Chapter 4 - Conditionals and Loops

This chapter outlines the structures in the ScriptX language for handling conditionals such as if and case, and loops such as for and repeat, as well as several expressions for control of these loops.

Conditionals: if and case

Conditionals are used to perform an action or combination of actions based on the outcome of a given test. ScriptX has three forms of conditionals:

If Conditional

ScriptX has two if expressions: if . . . do and if . . . then . . . else.

if conditional do trueExpression 
if conditional then trueExpression else falseExpression
Use if . . . do if you are only interested in the true condition, and if . . . then . . . else if both the true and false conditions must be handled. You can use if . . . then without the else statement, but the compiler will be waiting for the else statement, so you will not immediately see the result of an if . . . then statement in the Listener window. Once another expression has been entered, however, you will see the results of both expressions. If you type an if . . . then without the else and want to see the result right away, you can use the operator !!, which is legal only at the top level when an expression is complete.

The conditional part of the if expression is an expression that evaluates to either false or something other than false. In both forms of the if expression, trueExpression is evaluated if the value of conditional is not false. That is, the test expression does not have to evaluate specifically to the true object for trueExpression to be evaluated-it just has to evaluate to something other than false. Both trueExpression and falseExpression are often compound expressions.

Both forms of the if expression return a value-that value is the value of either trueExpression or falseExpression, depending on which one was evaluated. Because if expressions return a value, you can nest if expressions inside other expressions, such as an assignment.

If the test in the if . . . do expression returns false, the whole if expression returns undefined.

Here are some examples using if expressions:

global x := 1, y := 5, p
if x > 0 do x := 0

p := if x > 0 then x else 0

if x < y then x 
else (
print "x is greater than y, setting x to 0"
x := 0
)

case Conditional

Case expressions are used to switch between possible operations based on the result of evaluating an initial expression.

case [ test ] of
tag : expression
. . .
[ otherwise : expression ]
end
The value of test is compared to the value of each of the tags in the tagged expressions (the tag:expression clause) in succession using the equal global function as a test of object equality. When this comparison first evaluates to true, then the expression (often a compound expression) is evaluated. Once a match is found and the expression is evaluated, the case expression terminates and yields the value of that expression. No other comparisons are made. Note that the case expression, like the if expression, always yields a value. If there is no match, the value is undefined.

You can have any number of tag:expression clauses in a case expression. Whereas the expression can be any expression (including a compound expression), the tag is a limited form of expression, evaluated before the return value of test is matched against it. The tag can be one of the following:

The optional otherwise clause identifies the default case, if none of the tags result in true:

case pet of 
@cat: print "meow"
@dog: print "woof"
@bird: print "chirp"
@snake: print "hiss"
otherwise: print "mysterious animal noise"
end

bool := case i of
	0:false
	1:true
	otherwise: (print "out of range"; undefined)
end

If the test is omitted, the tags are expected to be expressions that evaluate to true or false. When the first tag expression to yield true is evaluated, the case expression terminates and yields the value of its corresponding expression, which it evaluates.

case of
	(isComparable a b): (
		case of
			(a > b): print "a is greater than b" 
			(a = b): print "a is equal to b" 
			otherwise: print "a is less than b" 
		end
	)
	otherwise: print "a and b are not comparable" 
end

If the otherwise tag is omitted and none of the other tags match the value of the test expression, the value of the case expression is undefined.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.